package com.java.service;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.java.entity.User;

/**
 * 人员服务
 * @author wqf
 * @version  Nov 24, 2011 11:51:06 AM
 */
@Transactional(propagation = Propagation.REQUIRED,readOnly=false, rollbackFor = Exception.class)
public class UserService extends BaseService{

	/**
	 * 编辑用户信息 (for dwr)
	 * @param user
	 */
	public void editUser(User user){
		if(null==user.getId()){
			user.setPassword("111111");
			this.addUser(user);
		}else{
//			User oldPeople=this.dao.loadObject(User.class, user.getId());
//			user.setPassword(oldPeople.getPassword());
			this.updateUser(user);
		}
	}	
	
	public void addUser(User people){
		this.dao.saveObject(people);
	}
		
	public void updateUser(User user){
		this.dao.updateObject(user);
	}	
		
	/**
	 * 删除用户 （for dwr）
	 * @param id
	 */
	public void deleteUserById(Long id){
		User user = this.dao.loadObject(User.class, id);
		if(user!=null){
			this.dao.deleteObject(user);
		}
	}
			
	public void deleteUserByIds(String ids){
		if(StringUtils.isNotBlank(ids)){
			String []id  = ids.split(",");
			for(String s:id){
				deleteUserById(Long.parseLong(s));
			}			
		}	
	}
	
	/**
	 * 获取用户 （for dwr）
	 * @param id
	 * @return
	 */
	public User getUserById(Long id){
		return this.dao.loadObject(User.class, id);		
	}
	
	public boolean existsAccount(String account){
		User user = this.dao.loadObject(User.class, "account=?",account);
		if(user!=null){
			return true;
		}
		return false;
	}
	
	public boolean existsCode(User u){
		User user = this.dao.loadObject(User.class, "id!=? and code=?",u.getId(),u.getCode());
		if(user!=null){
			return true;
		}
		return false;
	}
	
	public void resetPassword(Long id){
		User user = this.dao.loadObject(User.class, id);
		if(null!=user){
			user.setPassword("111111");
			this.dao.updateObject(user);
		}
	}
	
}
